package com.shoujifeng.snowmusic.player.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.shoujifeng.snowmusic.db.DatabaseUtil;
import com.shoujifeng.snowmusic.entity.Mp3Info;
import com.shoujifeng.snowmusic.player.http.ServerAccess;

import android.R.integer;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;

public class MediaUtil {
	/**
	 * 閿熸枻鎷烽敓鑺傝揪鎷烽敓鏂ゆ嫹鑿橀敓鏂ゆ嫹鑳侀敓绐栴垽鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹鎭敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷稬ist閿熸枻鎷烽敓鏂ゆ嫹
	 * 
	 * @return
	 */
	public List<Mp3Info> getMp3Infos(Context context) {
		
		List<Mp3Info> mp3Infos = new ArrayList<Mp3Info>();
		
		try {
			getDownloadMusic(context, mp3Infos);
			
			Cursor cursor = context.getContentResolver().query( //
					MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
					MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
			
			for (int i = 0; i < cursor.getCount(); i++) {
				cursor.moveToNext();
				Mp3Info mp3Info = new Mp3Info();
				long id = cursor.getLong(cursor
						.getColumnIndex(MediaStore.Audio.Media._ID));				//閿熸枻鎷烽敓鏂ゆ嫹id
				String title = cursor.getString((cursor	
						.getColumnIndex(MediaStore.Audio.Media.TITLE)));			//閿熸枻鎷烽敓琛楁唻鎷烽敓鏂ゆ嫹
				String artist = cursor.getString(cursor
						.getColumnIndex(MediaStore.Audio.Media.ARTIST));			//閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷�
				long duration = cursor.getLong(cursor
						.getColumnIndex(MediaStore.Audio.Media.DURATION));			//鏃堕敓鏂ゆ嫹
				long size = cursor.getLong(cursor
						.getColumnIndex(MediaStore.Audio.Media.SIZE));				//閿熶茎纭锋嫹閿熸枻鎷峰皬
				String url = cursor.getString(cursor
						.getColumnIndex(MediaStore.Audio.Media.DATA));				//閿熶茎纭锋嫹璺敓鏂ゆ嫹
				int isMusic = cursor.getInt(cursor
						.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));			//閿熻鍑ゆ嫹涓洪敓鏂ゆ嫹閿熸枻鎷�
				String album = cursor.getString(cursor  
		                    .getColumnIndex(MediaStore.Audio.Media.ALBUM));          //涓撻敓鏂ゆ嫹  
				if (isMusic != 0) {		//鍙敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷锋嫢閿熸枻鎷烽敓鏂ゆ嫹绯婚敓鏂ゆ嫹閿燂拷
					mp3Info.setId(id);
					mp3Info.setTitle(title);
					mp3Info.setArtist(artist);
					mp3Info.setDuration(duration);
					mp3Info.setSize(size);
					mp3Info.setUrl(url);
					mp3Info.setAlbum(album);
					mp3Infos.add(mp3Info);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	
		return mp3Infos;
	}
	
	public ArrayList<HashMap<String, Object>> getDownloadMusic(Context context, List<Mp3Info> mp3Infos) {
		DatabaseUtil databaseUtil = new DatabaseUtil(context);
		ArrayList<HashMap<String, Object>> array = databaseUtil.getDownload(-1, 1);
		
		for(int i = 0; i < array.size(); i++) {
			
			HashMap<String, Object> map = array.get(i);
			Mp3Info mp3Info = new Mp3Info();
			mp3Info.setId(Integer.parseInt(map.get(ServerAccess.UID).toString()));
			mp3Info.setTitle(map.get(ServerAccess.MUSIC_NAME).toString());
			mp3Info.setArtist(map.get(ServerAccess.SINGER).toString());
			mp3Info.setDuration(0);
			mp3Info.setSize(0);
			mp3Info.setUrl(map.get(ServerAccess.FILE_URL).toString());
			mp3Info.setAlbum("");
			
			mp3Infos.add(mp3Info);
		}
		return array;
	}
	
	/**
	 * 閿熸枻鎷稬ist閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熺祫ap閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷疯崺閿熺煫鎭充紮鎷烽敓绲刟p閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷蜂竴閿熸枻鎷烽敓鏂ゆ嫹閿熻纰夋嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹
	 * @param mp3Infos
	 * @return
	 */
	public static List<HashMap<String, String>> getMusicMaps(
			List<Mp3Info> mp3Infos) {
		List<HashMap<String, String>> mp3list = new ArrayList<HashMap<String, String>>();
		for (Iterator iterator = mp3Infos.iterator(); iterator.hasNext();) {
			Mp3Info mp3Info = (Mp3Info) iterator.next();
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("title", mp3Info.getTitle());
			map.put("Artist", mp3Info.getArtist());
			map.put("duration", formatTime(mp3Info.getDuration()));
			map.put("size", String.valueOf(mp3Info.getSize()));
			map.put("url", mp3Info.getUrl());
			map.put("album", mp3Info.getAlbum());
			mp3list.add(map);
		}
		return mp3list;
	}
	
	/**
	 * 閿熸枻鎷峰紡閿熸枻鎷锋椂閿熸垝锛岄敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹杞敓鏂ゆ嫹涓洪敓鏂ゆ嫹:閿熸枻鎷烽敓缁烇拷
	 * @param time
	 * @return
	 */
	public static String formatTime(long time) {
		String min = time / (1000 * 60) + "";
		String sec = time % (1000 * 60) + "";
		if (min.length() < 2) {
			min = "0" + time / (1000 * 60) + "";
		} else {
			min = time / (1000 * 60) + "";
		}
		if (sec.length() == 4) {
			sec = "0" + (time % (1000 * 60)) + "";
		} else if (sec.length() == 3) {
			sec = "00" + (time % (1000 * 60)) + "";
		} else if (sec.length() == 2) {
			sec = "000" + (time % (1000 * 60)) + "";
		} else if (sec.length() == 1) {
			sec = "0000" + (time % (1000 * 60)) + "";
		}
		return min + ":" + sec.trim().substring(0, 2);
	}
}
